﻿using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Castle.Core.Logging;
using Mt.Site.Core.AccessStatistics.Entities;
using Mt.Site.Core.Sql;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace Mt.Site.Core.AccessStatistics
{
    public class IpDataManager : IIpDataManager, ISingletonDependency
    {
        private readonly IRepository<IPInfo> _IPInfoRepository;
        private readonly IRepository<AccessGeneral> _accessGeneralRepository;
        private readonly IAppFolders _appFolders;

        private static bool HasImported = false;
        private readonly string FilePath;
        private readonly IUnitOfWorkManager _unitOfWorkManager;

        private readonly ISqlExecuter _sqlExecuter;

        public ILogger Logger { protected get; set; }
        private readonly static object _lockHelper = new object();

        public IpDataManager(
            IAppFolders appFolders, 
            IRepository<IPInfo>  IPInfoRepository,
            IRepository<AccessGeneral> accessGeneralRepository,
            IUnitOfWorkManager unitOfWorkManager,
            ISqlExecuter sqlExecuter)
        {
            Logger = NullLogger.Instance;
            _appFolders = appFolders;
            _IPInfoRepository = IPInfoRepository;
            _accessGeneralRepository = accessGeneralRepository;
            _unitOfWorkManager = unitOfWorkManager;

            _sqlExecuter = sqlExecuter;

            FilePath = Path.Combine(_appFolders.TempFolder, "Statics/ip.txt");
        }


        public void CheckIpRepository()
        {
            if (!HasImported)
            {
                if (CheckIsImportedFromDb())
                {
                    HasImported = true;
                }
                else
                {
                    lock (_lockHelper)
                    {
                        if (!HasImported)
                        {
                            if (File.Exists(FilePath))
                            {
                                if (ImportData())
                                {
                                    HasImported = true;
                                }
                            }
                        }
                    }
                }
            }
        }

        public IPInfo GetIPInfo(string ip)
        {
            var val = IpToInt(ip);
            return _IPInfoRepository.FirstOrDefault(c => c.StartDigit <= val && c.EndDigit >= val);
        }
        private bool ImportData()
        {
            ///写入数据前先清空表。
            CleanIpData();
            Logger.Debug("开始导入IP库数据。");

            string line;
            Regex IpPattern = new Regex(@"((1\d{2}|2[0-4]\d|25[0-5]|\d{1,2})\.){3}(1\d{2}|2[0-4]\d|25[0-5]|\d{1,2})");

            string[] sheng = new string[34] { "北京市", "天津市", "河北省", "山西省", "内蒙古", "辽宁省", "吉林省", "黑龙江省", "上海市", "江苏省", "浙江省", "安徽省", "福建省", "江西省", "山东省", "河南省", "湖北省", "湖南省", "广东省", "广西", "海南省", "重庆市", "四川省", "贵州省", "云南省", "西藏", "陕西省", "甘肃省", "青海省", "宁夏", "新疆", "香港", "澳门", "台湾省" };
            string[] shengvalue = new string[34] { "北京", "天津", "河北", "山西", "内蒙", "辽宁", "吉林", "黑龙江", "上海", "江苏", "浙江", "安徽", "福建", "江西", "山东", "河南", "湖北", "湖南", "广东", "广西", "海南", "重庆", "四川", "贵州", "云南", "西藏", "陕西", "甘肃", "青海", "宁夏", "新疆", "香港", "澳门", "台湾" };
            string[] shi = new string[34]{
            "北京市=北京",
            "天津市=天津",
            "石家庄市=石家庄|保定市=保定|唐山市=唐山|邯郸市=邯郸|邢台市=邢台|沧州市=沧州|衡水市=衡水|廊坊市=廊坊|承德市=承德|迁安市=迁安|鹿泉市=鹿泉|秦皇岛市=秦皇岛|南宫市=南宫市|任丘市=任丘|葉城市=葉城|辛集市=辛集|涿州市=涿州|定州市=定州|晋州市=晋州|霸州市=霸州|黄骅市=黄骅|遵化市=遵化|张家口市=张家口|沙河市=沙河市|三河市=三河|冀州市=冀州|武安市=武安|河间市=河间|深州市=深州|新乐市=新乐|泊头市=泊头|安国市=安国|高碑店市=高碑店",
            "太原市=太原|大同市=大同|阳泉市=阳泉|长治市=长治|临汾市=临汾|晋中市=晋中|运城市=运城|忻州市=忻州|朔州市=朔州|吕梁市=吕梁|古交市=古交|高平市=高平|永济市=永济|孝义市=孝义|侯马市=侯马|霍州市=霍州|介休市=介休|河津市=河津|汾阳市=汾阳|原平市=原平|潞城市=潞城",
           "呼和浩特市=呼和浩特|呼伦贝尔市=呼伦贝尔|赤峰市=赤峰|扎兰屯市=扎兰屯|鄂尔多斯市=鄂尔多斯|乌兰察布市=乌兰察布|巴彦淖尔市=巴彦淖尔|二连浩特市=二连浩特|霍林郭勒市=霍林郭勒|包头市=包头|乌海市=乌海|阿尔山市=阿尔山|乌兰浩特市=乌兰浩特|锡林浩特市=锡林浩特|根河市=根河|满洲里市=满洲里|额尔古纳市=额尔古纳|牙克石市=牙克石|临河市=临河|丰镇市=丰镇|通辽市=通辽",
           "沈阳市=沈阳|葫芦岛市=葫芦岛|大连市=大连|盘锦市=盘锦|鞍山市=鞍山|铁岭市=铁岭|本溪市=本溪|丹东市=丹东|抚顺市=抚顺|锦州市=锦州|辽阳市=辽阳|阜新市=阜新|调兵山市=调兵山|朝阳市=朝阳市|海城市=海城|北票市=北票|盖州市=盖州|凤城市=凤城|庄河市=庄河|凌源市=凌源|开原市=开原|兴城市=兴城|新民市=新民市|大石桥市=大石桥|东港市=东港市|北宁市=北宁|瓦房店市=瓦房店|普兰店市=普兰店|凌海市=凌海|灯塔市=灯塔|营口市=营口",
           "长春市=长春|吉林市=吉林市|通化市=通化|白城市=白城|四平市=四平|辽源市=辽源|松原市=松原|白山市=白山|集安市=集安|梅河口市=梅河口|双辽市=双辽|延吉市=延吉|九台市=九台|桦甸市=桦甸|榆树市=榆树|蛟河市=蛟河|磐石市=磐石|大安市=大安|德惠市=德惠|洮南市=洮南|龙井市=龙井|珲春市=珲春|公主岭市=公主岭|图们市=图们|舒兰市=舒兰|和龙市=和龙|临江市=临江|敦化市=敦化",
           "哈尔滨市=哈尔滨|伊春市=伊春|牡丹江市=牡丹江|大庆市=大庆|鸡西市=鸡西|鹤岗市=鹤岗|绥化市=绥化|齐齐哈尔市=齐齐哈尔|黑河市=黑河|富锦市=富锦|虎林市=虎林|密山市=密山|佳木斯市=佳木斯|双鸭山市=双鸭山|海林市=海林|铁力市=铁力|北安市=北安|五大连池市=五大连池|阿城市=阿城|尚志市=尚志|五常市=五常|安达市=安达|七台河市=七台河|绥芬河市=绥芬河|双城市=双城|海伦市=海伦|宁安市=宁安|讷河市=讷河|穆棱市=穆棱|同江市=同江|肇东市=肇东",
           "上海市=上海",
           "南京市=南京|无锡市=无锡|常州市=常州|扬州市=扬州|徐州市=徐州|苏州市=苏州|连云港市=连云港|盐城市=盐城|淮安市=淮安|宿迁市=宿迁|镇江市=镇江|南通市=南通|泰州市=泰州|兴化市=兴化|东台市=东台|常熟市=常熟|江阴市=江阴|张家港市=张家港|通州市=通州|宜兴市=宜兴|邳州市=邳州|海门市=海门|大丰市=大丰|溧阳市=溧阳|泰兴市=泰兴|如市=如市|昆山市=昆山|启东市=启东|江都市=江都|丹阳市=丹阳|吴江市=吴江|靖江市=靖江|扬中市=扬中|新沂市=新沂|仪征市=仪征|太仓市=太仓|姜堰市=姜堰|高邮市=高邮|金坛市=金坛|句容市=句容|灌南县=灌南",
        "杭州市=杭州|宁波市=宁波|绍兴市=绍兴|温州市=温州|台州市=台州|湖州市=湖州|嘉兴市=嘉兴|金华市=金华|舟山市=舟山|衢州市=衢州|丽水市=丽水|余姚市=余姚|乐清市=乐清|临海市=临海|温岭市=温岭|永康市=永康|瑞安市=瑞安|慈溪市=慈溪|义乌市=义乌|上虞市=上虞|诸暨市=诸暨|海宁市=海宁|桐乡市=桐乡|兰溪市=兰溪|龙泉市=龙泉|建德市=建德|富德市=富德|富阳市=富阳|平湖市=平湖|东阳市=东阳|嵊州市=嵊州|奉化市=奉化|临安市=临安|江山市=江山",
           "合肥市=合肥|毫州市=毫州|芜湖市=芜湖|马鞍山市=马鞍山|池州市=池州|黄山市=黄山|滁州市=滁州|安庆市=安庆|淮南市=淮南|淮北市=淮北|蚌埠市=蚌埠|巢湖市=巢湖|宿州市=宿州|宣城市=宣城|六安市=六安|阜阳市=阜阳|铜陵市=铜陵|明光市=明光|天长市=天长|宁国市=宁国|界首市=界首|桐城市=桐城",
        "福州市=福州|厦门市=厦门|泉州市=泉州|漳州市=漳州|南平市=南平|三明市=三明|龙岩市=龙岩|莆田市=莆田|宁德市=宁德|建瓯市=建瓯|武夷山市=武夷山|长乐市=长乐|福清市=福清|晋江市=晋江|南安市=南安|福安市=福安|龙海市=龙海|邵武市=邵武|石狮市=石狮|福鼎市=福鼎|建阳市=建阳|漳平市=漳平|永安市=永安",
          "南昌市=南昌|赣州市=赣州|上饶市=上饶|宜春市=宜春|景德镇市=景德镇|亲余市=亲余|九江市=九江|萍乡市=萍乡|抚州市=抚州|鹰潭市=鹰潭|吉安市=吉安|丰城市=丰城|樟树市=樟树|德兴市=德兴|瑞金市=瑞金|井冈山市=井冈山|高安市=高安|乐平市=乐平|南康市=南康|贵溪市=贵溪|瑞昌市=瑞昌|东乡县=东乡|广丰县=广丰|信州区=信州|三清山=三清",
         "济南市=济南|青岛市=青岛|威海市=威海|潍坊市=潍坊|菏泽市=菏泽|济宁市=济宁|莱芜市=莱芜|东营市=东营|烟台市=烟台|淄博市=淄博|枣庄市=枣庄|泰安市=泰安|临沂市=临沂|日照市=日照|德州市=德州|聊城市=聊城|滨州市=滨州|乐陵市=乐陵|兖州市=兖州|诸城市=诸城|邹城市=邹城|滕州市=滕州|肥城市=肥城|新泰市=新泰|胶州市=胶州|胶南市=胶南|即墨市=即墨|龙口市=龙口|平度市=平度|莱西市=莱西",
          "郑州市=郑州|洛阳市=洛阳|焦作市=焦作|商丘市=商丘|信阳市=信阳|周口市=周口|鹤壁市=鹤壁|安阳市=安阳|濮阳市=濮阳|驻马店市=驻马店|南阳市=南阳|开封市=开封|漯河市=漯河|许昌市=许昌|新乡市=新乡|济源市=济源|灵宝市=灵宝|偃师市=偃师|邓州市=邓州|登封市=登封|三门峡市=三门峡|新郑市=新郑|禹州市=禹州|巩义市=巩义|永城市=永城|长葛市=长葛|义马市=义马|林州市=林州|项城市=项城|汝州市=汝州|荥阳市=荥阳|平顶山市=平顶山|卫辉市=卫辉|辉县市=辉县|舞钢市=舞钢|新密市=新密|孟州市=孟州|沁阳市=沁阳|郏县=郏县",
         "武汉市=武汉|荆门市=荆门|咸宁市=咸宁|襄樊市=襄樊|荆州市=荆州|黄石市=黄石|宜昌市=宜昌|随州市=随州|鄂州市=鄂州|孝感市=孝感|黄冈市=黄冈|十堰市=十堰|枣阳市=枣阳|老河口市=老河口|恩施市=恩施|仙桃市=仙桃|天门市=天门|钟祥市=钟祥|潜江市=潜江|麻城市=麻城|洪湖市=洪湖|汉川市=汉川|赤壁市=赤壁|松滋市=松滋|丹江口市=丹江口|武穴市=武穴|广水市=广水|石首市=石首|大冶市=大冶|枝江市=枝江|应城市=应城|宜城市=宜城|当阳市=当阳|安陆市=安陆|宜都市=宜都|利川市=利川",
        "长沙市=长沙|郴州市=郴州|益阳市=益阳|娄底市=娄底|株洲市=株洲|衡阳市=衡阳|湘潭市=湘潭|岳阳市=岳阳|常德市=常德|邵阳市=邵阳|永州市=永州|张家界市=张家界|怀化市=怀化|浏阳市=浏阳|醴陵市=醴陵|湘乡市=湘乡|耒阳市=耒阳|沅江市=沅江|涟源市=涟源|常宁市=常宁|吉首市=吉首|津市市=津市|冷水江市=冷水江|临湘市=临湘|汨罗市=汨罗|武冈市=武冈|韶山市=韶山|安化县=安化|湘西州=湘西",
          "广州市=广州|深圳市=深圳|珠海市=珠海|汕头市=汕头|佛山市=佛山|韶关市=韶关|河源市=河源|梅州市=梅州|惠州市=惠州|汕尾市=汕尾|东莞市=东莞|中山市=中山|江门市=江门|阳江市=阳江|湛江市=湛江|茂名市=茂名|肇庆市=肇庆|清远市=清远|潮州市=潮州|揭阳市=揭阳|云浮市=云浮",
           "南宁市=南宁|贺州市=贺州|玉林市=玉林|桂林市=桂林|柳州市=柳州|梧州市=梧州|北海市=北海|钦州市=钦州|百色市=百色|防城港市=防城港|贵港市=贵港|河池市=河池|崇左市=崇左|来宾市=来宾|东兴市=东兴|桂平市=桂平|北流市=北流|岑溪市=岑溪|合山市=合山|凭祥市=凭祥|宜州市=宜州",
        "海口市=海口|三亚市=三亚|万宁市=万宁|文昌市=文昌|儋州市=儋州|琼海市=琼海|东方市=东方市|五指山市=五指山",
        "重庆市=重庆",
        "成都市=成都|广安市=广安|德阳市=德阳|乐山市=乐山|巴中市=巴中|内江市=内江|宜宾市=宜宾|南充市=南充|都江堰市=都江堰|自贡市=自贡|泸洲市=泸洲|广元市=广元|达州市=达州|资阳市=资阳|绵阳市=绵阳|眉山市=眉山|遂宁市=遂宁|雅安市=雅安|阆中市=阆中|攀枝花市=攀枝花|广汉市=广汉|绵竹市=绵竹|万源市=万源|华蓥市=华蓥|江油市=江油|西昌市=西昌|彭州市=彭州|简阳市=简阳|崇州市=崇州|什邡市=什邡|峨眉山市=峨眉山|邛崃市=邛崃|双流县=双流",
        "贵阳市=贵阳|安顺市=安顺|遵义市=遵义|六盘水市=六盘水|兴义市=兴义|都匀市=都匀|凯里市=凯里|毕节市=毕节|清镇市=清镇|铜仁市=铜仁|赤水市=赤水|仁怀市=仁怀|福泉市=福泉",
        "昆明市=昆明|玉溪市=玉溪|大理市=大理|曲靖市=曲靖|昭通市=昭通|保山市=保山|丽江市=丽江|临沧市=临沧|楚雄市=楚雄|开远市=开远|个旧市=个旧|景洪市=景洪|安宁市=安宁|宣威市=宣威",
        "拉萨市=拉萨|日喀则市=日喀则",
        "西安市=西安|咸阳市=咸阳|榆林市=榆林|宝鸡市=宝鸡|铜川市=铜川|渭南市=渭南|汉中市=汉中|安康市=安康|商洛市=商洛|延安市=延安|韩城市=韩城|兴平市=兴平|华阴市=华阴",
        "兰州市=兰州|白银市=白银|武威市=武威|金昌市=金昌|平凉市=平凉|张掖市=张掖|嘉峪关市=嘉峪关|酒泉市=酒泉|庆阳市=庆阳|定西市=定西|陇南市=陇南|天水市=天水|玉门市=玉门|临夏市=临夏|合作市=合作市|敦煌市=敦煌|甘南州=甘南",
        "西宁市=西宁|格尔木市=格尔木|德令哈市=德令哈",
       "银川市=银川|固原市=固原|石嘴山市=石嘴山|青铜峡市=青铜峡|中卫市=中卫|吴忠市=吴忠|灵武市=灵武",
        "乌鲁木齐市=乌鲁木齐|石河子市=石河子|喀什市=喀什|阿勒泰市=阿勒泰|阜康市=阜康|库尔勒市=库尔勒|阿克苏市=阿克苏|阿拉尔市=阿拉尔|哈密市=哈密|克拉玛依市=克拉玛依|昌吉市=昌吉|奎屯市=奎屯|米泉市=米泉|和田市=和田|",
        "香港=香港",
        "澳门=澳门",
        "台北市=台北|台南市=台南|台中市=台中|高雄市=高雄|桃源市=桃源"
        };
            int count = 0;
            int lineCount = 0;
            string errLine = "";
            bool isAccepted = false;
            List<IPInfo> cache = new List<IPInfo>();
            try
            {
                using (FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read))
                {
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        // 从文件读取并显示行，直到文件的末尾 
                        while ((line = sr.ReadLine()) != null)
                        {
                            lineCount++;
                            isAccepted = false;
                            if (IpPattern.Matches(line).Count >= 2)
                            {
                                string[] strs = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                if (strs.Length >= 2)
                                {
                                    if (IpPattern.IsMatch(strs[0]) && IpPattern.IsMatch(strs[1]))
                                    {
                                        count++;
                                        isAccepted = true;
                                        InsertIPSpanData(cache, strs, sheng, shengvalue, shi);
                                    }
                                }
                            }
                            if(isAccepted == false)
                            {
                                errLine += ","+ lineCount;
                            }
                        }
                    }
                }
                SaveIpEntitiesIntoDb(cache);
                Logger.Debug($"共读取{lineCount}行数据,导入{count}条IP数据！,以下几行数据未导入：{errLine}");
                SetIsImportedIntoDb();
                return true;
            }
            catch(Exception ex)
            {
                Logger.Error($"已读取到第{lineCount}行数据！",ex);
                return false;
            }
        }
        private void CleanIpData()
        {
            using (var uow = _unitOfWorkManager.Begin())
            {
                _sqlExecuter.Execute("truncate table AppIPInfos");
                uow.Complete();
            }
        }
        private bool CheckIsImportedFromDb()
        {
            bool result = false;
            using (var uow = _unitOfWorkManager.Begin())
            {
                var model = _accessGeneralRepository.FirstOrDefault(c => c.Id > 0);
                if (model != null)
                {
                    result = model.HasImportIpData;
                }
                uow.Complete();
            }
            return result;
        }
        private void SetIsImportedIntoDb()
        {
            using (var uow = _unitOfWorkManager.Begin())
            {
                var model = _accessGeneralRepository.FirstOrDefault(c => c.Id > 0);
                if (model == null)
                {
                    model = new AccessGeneral();
                }
                model.HasImportIpData = true;
                if (model.IsTransient())
                {
                    _accessGeneralRepository.Insert(model);
                }
                else
                {
                    _accessGeneralRepository.Update(model);
                }
                uow.Complete();
            }
        }
        private void InsertIPSpanData(List<IPInfo> cache, string[] strs, string[] sheng, string[] shengvalue, string[] shi)
        {
            long ip1 = IpToInt(strs[0]);
            long ip2 = IpToInt(strs[1]);

            string address, ISP;
            if (strs.Length > 2)
            {
                address = strs[2];
                if (strs.Length == 3)
                {
                    ISP = "未知";
                }
                else
                {
                    ISP = string.Join(" ", strs.Skip(3));
                    if (ISP == "CZ88.NET")
                    {
                        ISP = "未知";
                    }
                }
            }
            else
            {
                address = "未知";
                ISP = "未知";
            }

            var entity = new IPInfo()
            {
                StartIp = strs[0],
                EndIp = strs[1],
                StartDigit = ip1,
                EndDigit = ip2
            };
            Analysis(entity, address, ISP, sheng, shengvalue, shi);

            cache.Add(entity);
            if (cache.Count > 1000) //分批保存（一次太多会事务超时）
            {
                SaveIpEntitiesIntoDb(cache);
            }
        }
        private void SaveIpEntitiesIntoDb(List<IPInfo> cache)
        {
            if(cache.Count > 0)
            {
                using (var uow = _unitOfWorkManager.Begin())
                {
                    foreach (var item in cache)
                    {
                        _IPInfoRepository.Insert(item);
                    }
                    uow.Complete();
                }
            }
            cache.Clear();
        }

        private void Analysis(IPInfo entity,string address,string ISP, string[] sheng, string[] shengvalue, string[] shi)
        { 
            bool inChina = true;

            int pi = 0;
            //省
            for (; pi < shengvalue.Length; pi++)
            {
                if (address.IndexOf(shengvalue[pi]) > -1)
                {
                    //找到匹配省份
                    entity.Region = sheng[pi];
                    //省内查找地级市
                    int si = 0;
                    string[] shis = shi[pi].Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                    for (; si < shis.Length; si++)
                    {
                        string[] stt = shis[si].Split('=');
                        if (address.IndexOf(stt[1]) > -1)
                        {
                            entity.City = stt[0];//找到匹配城市
                            break;
                        }
                    }
                    if (si >= shis.Length)
                        entity.City = "其他";
                    break;
                }
            }
            if (pi >= shengvalue.Length)
            {
                //全体范围查找市
                for (pi = 0; pi < shengvalue.Length; pi++)
                {
                    int si = 0;
                    string[] shis = shi[pi].Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                    for (; si < shis.Length; si++)
                    {
                        string[] stt = shis[si].Split('=');
                        if (address.IndexOf(stt[1]) > -1)
                        {
                            entity.City = stt[0];//找到匹配城市
                            entity.Region = sheng[pi];//对应省份
                            break;
                        }
                    }
                    if (si >= shis.Length)
                    {
                        inChina = false;
                        entity.City = "其他";
                        entity.Region = address;
                    }
                }
            }

            //运营商
            entity.ISP = "其他";
            if (inChina)//国内查找
            {
                if (ISP != "未知")
                {
                    if ((ISP.IndexOf("学") > -1 && ISP.IndexOf("网吧") < 0) || ISP.IndexOf("教育网") > -1 || ISP.IndexOf("CERNET") > -1)
                    {
                        entity.ISP = "教育网";
                    }
                    else if (ISP.IndexOf("电信通") > -1) { entity.ISP = "电信通"; }
                    else if (ISP.IndexOf("电信") > -1) { entity.ISP = "电信"; }
                    else if (ISP.IndexOf("联通") > -1) { entity.ISP = "联通"; }
                    else if (ISP.IndexOf("移动") > -1) { entity.ISP = "移动"; }
                    else if (ISP.IndexOf("华数宽带") > -1) { entity.ISP = "华数宽带"; }
                    else if (ISP.IndexOf("长城宽带") > -1) { entity.ISP = "长城宽带"; }
                    else if (ISP.IndexOf("铁通") > -1) { entity.ISP = "铁通"; }
                    else if (ISP.IndexOf("电讯盈科") > -1) { entity.ISP = "电讯盈科"; }
                    else if (ISP.IndexOf("城市电讯") > -1) { entity.ISP = "城市电讯"; }
                    else if (ISP.IndexOf("方正宽带") > -1) { entity.ISP = "方正宽带"; }
                }
                else if (address.IndexOf("学") > -1 && address.IndexOf("网吧") < 0)
                {
                    entity.ISP = "教育网";
                }
            }
        }

        private static long IpToInt(string ip)//IP地址转换成Int数据
        {
            char[] dot = new char[] { '.' };
            string[] ipArr = ip.Split(dot);
            if (ipArr.Length == 3)
            {
                ip = ip + ".0";
                ipArr = ip.Split(dot);
            }
            if(ipArr.Length != 4)
            {
                return 0;
            }
            long ip_Int = 0;
            long p1 = long.Parse(ipArr[0]) * 256 * 256 * 256;
            long p2 = long.Parse(ipArr[1]) * 256 * 256;
            long p3 = long.Parse(ipArr[2]) * 256;
            long p4 = long.Parse(ipArr[3]);
            ip_Int = p1 + p2 + p3 + p4;
            return ip_Int;
        }
        private static string IntToIP(long ip_Int)//int转换成IP
        {
            long seg1 = (ip_Int & 0xff000000) >> 24;
            if (seg1 < 0)
                seg1 += 0x100;
            long seg2 = (ip_Int & 0x00ff0000) >> 16;
            if (seg2 < 0)
                seg2 += 0x100;
            long seg3 = (ip_Int & 0x0000ff00) >> 8;
            if (seg3 < 0)
                seg3 += 0x100;
            long seg4 = (ip_Int & 0x000000ff);
            if (seg4 < 0)
                seg4 += 0x100;
            string ip = seg1.ToString() + "." + seg2.ToString() + "." + seg3.ToString() + "." + seg4.ToString();
            return ip;
        }
    }
}
